﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObTookit.Auto
{
    public class InfoCoder : BaseCoder
    {
        public string CreateCode()
        {
            string text = ReadTemplate(Template);
            text = text.Replace("[#AUTHOR#]", Author);
            text = text.Replace("[#CREATED#]", CreateDate);
            text = text.Replace("[#ModelNS#]", AutoObj.OmNS);
            text = text.Replace("[#TbDescir#]", AutoObj.TbDescri);
            text = text.Replace("[#ClsName#]", AutoObj.ClsName);

            text = text.Replace("[#DBAlias#]", AutoObj.DbAlias);
            text = text.Replace("[#TBName#]", AutoObj.TbName);
            text = text.Replace("[##FOR_STRUCTURE##]", CreateStructures());
            text = text.Replace("[##FOR_PROP##]", CreateProps());

            text = text.Replace("[##EXT_MODEL##]", "");
            text = text.Replace("[##STATUS##]", "");
            return text;
        }

        public string CreateStructures()
        {
            //[#PrivateProp#] = new [#ModelFieldType#](this, "[#FieldName#]", "[#FieldAlias#]")
            StringBuilder sb = new StringBuilder();
            foreach (FieldObject fo in FieldObjs)
            {
                sb.Append(CodeArgs.RN + CodeArgs.TAB4
                    + fo.PrivateProp + " = new "
                    + fo.Type + "(this, \""
                    + fo.Name + "\" , \""
                    + fo.Alias + "\"),");
            }

            return sb.Remove(sb.ToString().LastIndexOf(','), 1).ToString();
        }

        public string CreateProps()
        {
            //private [#ModelFieldType#] [#PrivateProp#];
            ///// <summary>
            ///// [#ModelFieldDescri#]
            ///// </summary>
            //public [#ModelFieldType#] [#PublicProp#] { get { return [#PrivateProp#]; } }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("#region [实体属性]" + CodeArgs.RN);
            foreach (FieldObject fo in FieldObjs)
            {
                sb.AppendLine(CodeArgs.TAB2 + "private " + fo.Type + " " + fo.PrivateProp + ";");
                sb.AppendLine(CodeArgs.TAB2 + "/// <summary>");
                sb.AppendLine(CodeArgs.TAB2 + "/// " + fo.Descr);
                sb.AppendLine(CodeArgs.TAB2 + "/// </summary>");
                sb.AppendLine(CodeArgs.TAB2 + "public " + fo.Type + " " + fo.PublicProp + " {"
                    + " get { return " + fo.PrivateProp + "; }}" + CodeArgs.RN);
            }
            sb.AppendLine(CodeArgs.TAB2 + "#endregion [实体属性]");

            return sb.ToString();
        }

        /// <summary>
        /// 扩展实体属性：EntityField
        /// </summary>
        /// <returns></returns>
        public string CreateExtModelInfo(string extModelName)
        {
            return null;
        }
        /// <summary>
        /// 枚举状态属性：EnumField
        /// </summary>
        /// <returns></returns>
        public string CreateStatus()
        {
            /*
             * 	public enum [#ClsName#]Status
                {
                    /// <summary>
                    /// 缺省状态：初始状态
                    /// </summary>
                    DEFAULT,
                    /// <summary>
                    /// 正常状态
                    /// </summary>
                    NORMAL,
                    /// <summary>
                    /// 删除状态
                    /// </summary>
                    DELETED
                }
             */
            return null;
        }
    }
}
